#ifndef BOOKCARINTERFACE_H
#define BOOKCARINTERFACE_H

#include <QWidget>
#include "globalParameterForTaxi.h"
#include "interface/IInterface.h"
#include "qtablewidget.h"
#include <QTimer>
#include <vector>
#include "socket/protocol/CarRequestPassenger.h"
#include "mapprocess/mapprocessor.h"
using namespace std;

namespace Ui {
    class ChooseClient;
}

namespace interface
{


//======================================================================
// Class:   CChooseCarInterface
//
// Description:
//      This class is responsible for show out the choose-client interface.
//      In this class, we also have a timer to send request to server for fetching other
//      willing-to-take-taxi passengers' information, and append them to the table widget.
//
//  Notes:
//	None.
//
#define COLUMNCOUNT 4
#define TIMEOUT 5000 // every 5 seconds will emit a signal to invoke timer
class CChooseClientInterface : public IInterface
{
    Q_OBJECT

public:
    explicit CChooseClientInterface(IInterface *parent = 0);
    ~CChooseClientInterface();

private Q_SLOTS:
    // button OK
    void chooseTheClient();
    // button CANCEL
    void cancelChoose();
    // when the communication between server and client is over,
    // this function will be invoked
    void readResult();
    // an error occures when communicating with server
    void socketError();

    // slots for table widget
    // when an item of the table has been clicked, this function will be invoked.
    void SelectTableItem( int row, int column );

    // when the timer is timeout, this slot will be invoked
    void sendRequestByTime();

    void mapLoaded();

private:
    // prepare for the background setting
    virtual void UiSetting(Ui::ChooseClient *ui);
    // fill the table
    void FillTable(QTableWidget * pTableView, QMap<QString, QString> * pCarInfoMap);
    // format the table
    void FormatTable(QTableWidget * pTableView);

    static void decreaseClientNum() {m_sAvailableClientNum--; }
    static void increaseClientNum() {m_sAvailableClientNum++; }
    static int getClientNum() {return m_sAvailableClientNum; }
    static void clearClientNum() {m_sAvailableClientNum = 0; }

private:
    Ui::ChooseClient *m_pUi;
    MapProcessor * mapProcessor;
    bool bMapLoad;
    protocal::CarRequestPassenger *m_pCarRequestPassenger;

    int m_chosenPassenger; // it is the row number of the table cell

    // a timer that will send the request every TIMEOUT ms to request for
    // other taxi infomation if there exist
    QTimer *m_pTimer;

    //items for table view
    vector<vector<QTableWidgetItem*> > m_vecItem;

    // a map between each table row and their corresponding car_id
    QMap<int, QString> m_mapRowToClientID;

    // a variaty that controls the number of current available clients
    static int m_sAvailableClientNum;

    // enum for tale column order, which must aglin to the extract order in the table
    enum
    {
        CLIENT_ID = 0,
        CLIENT_NAME,
        EXPECT_TIME,
        TEL
    };
};
}

#endif // BOOKCARINTERFACE_H
